from abc import abstractmethod

from service import bs_service
from service.bs_service import BsContext


class Strategy:
    def __init__(self, name, data):
        self.name = name
        self.data = data
        self.code = ''
        self.stock_name = ''
        self.result = ''
        self.resp = {'status': '0000', 'msg': 'success'}

    # 执行策略
    @abstractmethod
    def execute(self):
        print('开始执行策略' + str(self.name))

    # 添加返回参数
    def resp(self, status, msg):
        self.resp['status'] = status
        self.resp['msg'] = msg


class Strategy1(Strategy):
    # 资金和均线组合1
    # 条件：1.出现连续两个买入信号
    #       2.跌破五日均线和十日均线或者主力资金卖出
    def execute(self):
        task_c_list = self.data
        range_len = len(task_c_list)
        bs_context = BsContext(self.code, self.stock_name)
        for i in range(range_len - 1):
            tci0 = task_c_list[i + 1]
            tci1 = task_c_list[i]
            if (tci0.bs == 1 and tci1.bs == 1) and \
                    ((tci0.break_ave5 and tci1.break_ave5) or
                     (tci0.break_ave10 and tci1.break_ave10) or
                     (tci0.main_io_buy and tci1.main_io_buy)):
                bs_service.buy(bs_context, 100, tci0.rt_price, tci0.rq)
            elif tci0.bs == 2 and ((tci0.rt_price < tci0.ave10 and tci0.rt_price < tci0.ave5) or tci0.main_io_sell):
                bs_service.sell(bs_context, 0, tci0.rt_price, tci0.rq)
        if len(bs_context.deal_info) < 2:
            return
        for info in bs_context.deal_info:
            print(info)
        self.result = bs_context


class Strategy2(Strategy):
    # 资金和均线组合2
    # 条件：1.出现连续两个买入信号
    #       2.跌破五日均线和十日均线或者主力资金卖出
    def execute(self):
        task_c_list = self.data
        range_len = len(task_c_list)
        bs_context = BsContext(self.code, self.stock_name)
        for i in range(range_len - 1):
            tci0 = task_c_list[i + 1]
            tci1 = task_c_list[i]
            if (tci0.bs == 1 and tci1.bs == 1) and \
                    ((tci0.break_ave5 and tci1.break_ave5) or
                     (tci0.break_ave10 and tci1.break_ave10) or
                     (tci0.main_io_buy and tci1.main_io_buy)):
                bs_service.buy(bs_context, 100, tci0.rt_price, tci0.rq)
            elif tci0.bs == 2 and ((tci0.rt_price < tci0.ave10 and tci0.rt_price < tci0.ave5) or tci0.main_io_sell):
                bs_service.sell(bs_context, 0, tci0.rt_price, tci0.rq)
        if len(bs_context.deal_info) < 2:
            return
        for info in bs_context.deal_info:
            print(info)
        self.result = bs_context


class Strategy3(Strategy):
    # 资金和均线组合3
    # 条件：1.出现连续两个买入信号
    #       2.跌破五日均线和十日均线或者主力资金卖出
    def execute(self):
        task_c_list = self.data
        range_len = len(task_c_list)
        bs_context = BsContext(self.code, self.stock_name)
        for i in range(range_len - 1):
            tci0 = task_c_list[i + 1]
            tci1 = task_c_list[i]
            if (tci0.bs == 1 and tci1.bs == 1) and \
                    ((tci0.break_ave5 and tci1.break_ave5) or
                     (tci0.break_ave10 and tci1.break_ave10) or
                     (tci0.main_io_buy and tci1.main_io_buy)):
                bs_service.buy(bs_context, 100, tci0.rt_price, tci0.rq)
            elif tci0.bs == 2 and (tci0.below_ave10 or tci0.below_ave5 or tci0.main_io_sell):
                bs_service.sell(bs_context, 0, tci0.rt_price, tci0.rq)
        if len(bs_context.deal_info) < 2:
            return
        for info in bs_context.deal_info:
            print(info)
        self.result = bs_context
